# File：反射.py
# Author：信思智学
# Date ：2025/8/7 11:37
# Info：

"""
反射：
    getattr()   setattr()  hasattr()  delattr()

    01：操作类属性和类方法、静态方法
        获取属性：getattr(类名,"属性名称字符串")
        设置属性：setattr(类名,"属性名称字符串",需要设置的新的值)

        获取方法：getattr(类名,"方法名称字符串")
        执行方法：getattr(类名,"方法名称字符串")()

    02：操作对象属性和对象方法
        获取属性：getattr(对象名称,"属性名称字符串")
        执行方法：getattr(对象名称,"方法名称字符串")(参数)

    03：操作第三方模块
        执行方法：getattr(模块名称,"方法名称字符串")(参数)

    04：自己的模块
        执行方法：getattr(sys.modules["__main__"],"方法名称字符串")(参数)

"""

def add():
    print("加法运算")


import sys
# print(sys.modules["__main__"])
mymodel = sys.modules["__main__"]
# mymodel.add()

getattr(mymodel,"add")()









# import time
# # time.sleep(3)
# getattr(time,"sleep")(4)
# print("停留4秒后执行")


class Person:

    person_num1 = "1"
    person_num2 = "2"
    person_num3 = "3"
    person_num4 = "4"

    @classmethod
    def help(cls):
        print("帮助动作")

    @classmethod
    def login(cls):
        print("登录动作")

    @classmethod
    def get(cls):
        print("get请求动作")


    def __init__(self,username):
        self.username = username

    def study(self,project):
        print(self.username + "正在学习%s..."%project)


print(hasattr(Person,"login"))
#
#
# lufei = Person("路飞")
# # lufei.study("Python")
#
# getattr(lufei,"study")("反射技术")


# 传统做法
# num = input("请输入您要查询的编号...")
# if num == "person_num1":
#     print(Person.person_num1)
# if num == "person_num2":
#     print(Person.person_num2)
# if num == "person_num3":
#     print(Person.person_num3)


# 传统做法
# print(Person.person_num1)
# Person.person_num1 = 456
# print(Person.person_num1)
# Person.help()

# 反射做法
# print(getattr(Person,"person_num1"))
# setattr(Person,"person_num1",789)
# print(getattr(Person,"person_num1"))
#
# num = input("请输入您要查询的编号...")
# print(getattr(Person,num))

# print(getattr(Person,"help"))

# method = input("请输入您要执行的方法名称...")
# getattr(Person,method)()